//
//  GTWeChatDynamicAtView.m
//  GTW
//
//  Created by Ran on 15/11/12.
//  Copyright (c) 2015年 xcode. All rights reserved.
//

#import "GTWeChatDynamicAtView.h"
#import <TYAttributedLabel/TYAttributedLabel.h>
#import "RanTestModel.h"
#import "GTWeChatDynamicTool.h"
#import "GTWeChatDynamicSettings.h"
#import "GTWeChatDynamicTuple.h"

#pragma mark - ------------------------Model------------------------

@interface GTWeChatDynamicAtViewModel: NSObject

@property(nonatomic, strong)RanTestModel *sourceModel;
@property(nonatomic, assign)CGFloat theWidth;

@property(nonatomic, assign)CGRect atLabelFrame;
@property(nonatomic, strong)TYTextContainer *atTextContainer;

@end

@implementation GTWeChatDynamicAtViewModel

@end

#pragma mark - ------------------------View------------------------

@interface GTWeChatDynamicAtView()

@property(nonatomic, strong)TYAttributedLabel *atLabel;

@end

@implementation GTWeChatDynamicAtView

#pragma mark - Protocol

+ (GTWeChatDynamicTuple *)getProcessedDataFromSourceData: (id)sourceData withWidth: (CGFloat)theWidth
{
    GTWeChatDynamicAtViewModel *model = [[GTWeChatDynamicAtViewModel alloc] init];
    model.sourceModel = (RanTestModel *)sourceData;
    model.theWidth = theWidth;
    
    CGFloat viewHeight;
    CGFloat rowHeight = [GTWeChatDynamicTool getTextHeightWithAttribute:[GTWeChatDynamicSettings atTextAttributes]];
    TYTextContainer *container;
    
    
    if (model.sourceModel.atOnes.count){
        container = [[TYTextContainer alloc] init];
        [container appendImage:[UIImage imageNamed:@"Logo"] size:CGSizeMake(rowHeight, rowHeight)];
        __block NSString *spaceLeft;
        __block NSString *name;
        [model.sourceModel.atOnes enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) {
            if (idx == 0 && idx != model.sourceModel.atOnes.count - 1) {
                name = [NSString stringWithFormat:@"%@;", dic[@"name"]];
                spaceLeft = @"   ";
            }else if(idx == model.sourceModel.atOnes.count - 1){
                name = [NSString stringWithFormat:@"%@", dic[@"name"]];
                spaceLeft = @"  ";
            }else{
                name = [NSString stringWithFormat:@"%@;", dic[@"name"]];
                spaceLeft = @"  ";
            }
            [container appendText:spaceLeft];
            [container appendLinkWithText:name linkFont:[GTWeChatDynamicSettings atTextAttributes][NSFontAttributeName] linkColor:[GTWeChatDynamicSettings atTextAttributes][NSForegroundColorAttributeName] underLineStyle:kCTUnderlineStyleNone linkData:dic];
        }];
        container = [container createTextContainerWithTextWidth:theWidth];
        viewHeight = container.textHeight;
    }else{
        container = nil;
        viewHeight = 0;
    }
    
    model.atTextContainer = container;
    model.atLabelFrame = CGRectMake(0, 0, theWidth, viewHeight);
    
    GTWeChatDynamicTuple *tuple = [[GTWeChatDynamicTuple alloc] init];
    tuple.data = model;
    tuple.height = viewHeight;
    return tuple;
}

- (void)feedbackStoreData:(id)data
{
    GTWeChatDynamicAtViewModel *model = (GTWeChatDynamicAtViewModel *)data;
    self.atLabel.frame = model.atLabelFrame;
    self.atLabel.textContainer = model.atTextContainer;
}

#pragma mark - Getter

- (TYAttributedLabel *)atLabel
{
    if (!_atLabel) {
        self.atLabel = ({
            TYAttributedLabel *atLabel = [[TYAttributedLabel alloc] init];
            atLabel.lineBreakMode = kCTLineBreakByCharWrapping;
            [self addSubview:atLabel];
            atLabel;
        });
    }
    return _atLabel;
}

@end
